package controller;

import com.alibaba.fastjson.JSON;
import com.mysql.cj.util.StringUtils;
import entity.Admin;
import service.AdminService;
import service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = "/adminController")
public class AdminController extends HttpServlet {
    private AdminService adminService = new AdminServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String op = request.getParameter("op");
        AdminService a = new AdminServiceImpl();
        HttpSession session = request.getSession(true);
        if ("query".equals(op)) {
            String userId = request.getParameter("userId");
            String username = request.getParameter("username");
            String pageStr = request.getParameter("page");
            int page = StringUtils.isNullOrEmpty(pageStr) ? 0 : Integer.parseInt(pageStr);
            int pageSize = 2; // 每页2条记录

            try {
                int totalCount = adminService.getTotalCount(
                        userId != null && !userId.isEmpty() ? Integer.parseInt(userId) : null,
                        username
                );
                int totalPages = (totalCount + pageSize - 1) / pageSize;
                page = Math.max(0, Math.min(page, totalPages - 1));

                List<Admin> list = adminService.chaxun(
                        userId != null && !userId.isEmpty() ? Integer.parseInt(userId) : null,
                        username,
                        page,
                        pageSize
                );

                Map<String, Object> result = new HashMap<>();
                result.put("list", list);
                result.put("page", page);
                result.put("totalPages", totalPages);
                result.put("totalCount", totalCount);

                out.write(JSON.toJSONString(result));
            } catch (Exception e) {
                e.printStackTrace();
                out.write("{\"error\":\"查询失败\"}");
            }

    }else if("xiugai".equals(op)){
            try {
                Admin admin = new Admin();
                admin.setUserId(Integer.parseInt(request.getParameter("userId")));
                admin.setUsername(request.getParameter("username"));
                admin.setPhone(request.getParameter("phone"));
                admin.setSex(request.getParameter("sex"));

                int result = adminService.xiugai(admin);
                out.write("{\"success\":" + (result > 0) + "}");
            } catch (Exception e) {
                e.printStackTrace();
                out.write("{\"success\":false,\"message\":\"修改失败\"}");
            }
        }else if("shanchu".equals(op)){
    try {
        int userId = Integer.parseInt(request.getParameter("userId"));
        int result = adminService.shanchu(userId);
        out.write("{\"success\":" + (result > 0) + "}");
    } catch (Exception e) {
        e.printStackTrace();
        out.write("{\"success\":false,\"message\":\"删除失败\"}");
    }
} else if("tianjia".equals(op)){
    try {
        Admin admin = new Admin();
        admin.setUsername(request.getParameter("username"));
        admin.setPhone(request.getParameter("phone"));
        admin.setSex(request.getParameter("sex"));
        admin.setPassword(request.getParameter("password"));

        int result = adminService.tianjia(admin);
        out.write("{\"success\":" + (result > 0) + "}");
    } catch (Exception e) {
        e.printStackTrace();
        out.write("{\"success\":false,\"message\":\"添加失败\"}");
    }
}
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        doPost(request, response);
    }
}

